#include "chartfigure.h"
#include "ui_chartfigure.h"
#include "serial.h"

ChartFigure::ChartFigure(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ChartFigure)
{
    ui->setupUi(this);
    chart_init();      //图表初始化

    /*---------------------------曲线设计信号与槽(Begin)-------------------------------------*/
//    chart_timer = new QTimer(this);                                   //创建定时器
//    connect(chart_timer,SIGNAL(timeout()),this,SLOT(DrawLine()));     //连接定时器与定时溢出处理槽函数
    connect(ui->btn_chart_theme_dark,  &QPushButton::clicked, this, &ChartFigure::btn_chart_dark_click);       //设置深色主题
    connect(ui->btn_chart_theme_light,  &QPushButton::clicked, this, &ChartFigure::btn_chart_light_click);     //设置浅色主题
    connect(ui->cb_temper, SIGNAL(clicked()), this, SLOT(cb_temper_show()));  //温度曲线复选框槽函数
    connect(ui->cb_flow, SIGNAL(clicked()), this, SLOT(cb_flow_show()));      //流量曲线复选框槽函数
    connect(ui->cb_show_all, SIGNAL(clicked()), this, SLOT(cb_show_all()));   //全部曲线显示复选框槽函数
    connect(ui->cb_hide_all, SIGNAL(clicked()), this, SLOT(cb_hide_all()));   //全部曲线不显示复选框槽函数
    /*---------------------------曲线设计信号与槽(End)-------------------------------------*/
}

/**
 * @brief 图表初始化
 */
void ChartFigure::chart_init()
{
        QPen penY(Qt::darkBlue,3,Qt::SolidLine,Qt::RoundCap,Qt::RoundJoin);
        chart = new QChart();
        series1 = new QSplineSeries;
        series2 = new QSplineSeries;
        axisX = new QDateTimeAxis();
        axisY = new QValueAxis();

        chart->legend()->setVisible(true);               //显示图例
        chart->legend()->setAlignment(Qt::AlignTop);     //底部对齐
//        chart->setTitle("实时数据");                      //设置标题
        chart->setTitleBrush(QBrush(QColor(0,0,255)));   //设置标题Brush
        chart->setTitleFont(QFont("微软雅黑"));           //设置标题字体
        chart->setTheme(QChart::ChartThemeBrownSand);    //主题设置
        chart->addSeries(series1);                       //把线添加到chart
        chart->addSeries(series2);
        axisX->setTickCount(10);                         //设置坐标轴格数
        axisY->setTickCount(10);
        axisX->setFormat("hh:mm:ss");                    //设置时间显示格式
        axisY->setMin(0);                                //设置Y轴范围
        axisY->setMax(10);
        axisX->setTitleText("Time(h:m:s)");              //设置X轴名称
        axisY->setGridLineVisible(true);                 //设置Y轴网格显示
//        axisY->setLinePen(penY);
//        axisX->setLinePen(penY);
        axisY->setTitleText("Data");
//        axisY->setLabelFormat("%d kg");       //y轴按照格式输出
        chart->addAxis(axisX,Qt::AlignBottom);  //设置坐标轴位于chart中的位置
//        chart->setAxisX(axisX,series1);
        chart->addAxis(axisY,Qt::AlignLeft);
//        chart->setBackgroundBrush(QBrush(QColor(64, 64, 64)));     // 设置统计图背景


//        series1->setUseOpenGL(true);                   //设置使用OpenGL加速
        series1->attachAxis(axisX);                      //把数据添加到坐标轴上
        series1->attachAxis(axisY);
        series1->setName("温度");
        series1->setPointLabelsFormat("@yPoint ℃");     //拐点处显示数值的格式
        series1->setPointLabelsVisible(true);            //拐点处显示数值
        series1->setPointsVisible(true);                 //拐点处凸起
        series1->setPointLabelsColor(QColor(0, 0, 204)); //设置显示数值的颜色
        series1->setPen(QPen(QColor(60, 187, 251), 2));  //设置曲线的颜色和粗细
//        series2->setUseOpenGL(true);                     //设置使用OpenGL加速，开启后曲线无法隐藏
        series2->attachAxis(axisX);                      //把数据添加到坐标轴上
        series2->attachAxis(axisY);
        series2->setName("流量");
        series2->setPointLabelsFormat("@yPoint");
        series2->setPointLabelsVisible(true);
        series2->setPointsVisible(true);
        series2->setPen(QPen(QColor(51, 102, 204), 3));


        //把chart显示到窗口上
        ui->widget->setChart(chart);
        ui->widget->setRenderHint(QPainter::Antialiasing);   //设置抗锯齿,曲线平滑
}


/**
 * @brief 画点、动态更新数据
 */
void ChartFigure::DrawLine(qreal x, qreal y1, qreal y2)
{
//    qDebug()<<x<<"\t"<<y1<<"\t"<<y2;
    //设置坐标轴显示范围
    chart->axisX()->setMin(QDateTime::currentDateTime().addSecs(-9 * 1));       //系统当前时间的前一秒，秒更新时配合X轴点数进行设置
    chart->axisX()->setMax(QDateTime::currentDateTime().addSecs(0));            //系统当前时间

    if(Serial::serialClass->unit_temper_ssd){
        series1->setPointLabelsFormat("@yPoint ℃");     //拐点处显示数值的格式
    }else{
        series1->setPointLabelsFormat("@yPoint ℉");     //拐点处显示数值的格式
        y1 = 32+y1*1.8;
    }

    //y轴自适应
    if(y1 > 10){
        axisY->setMax(y1+5);
    }else{
        axisY->setMax(10);
    }
    if(y1 < 0){
        axisY->setMin(y1-5);
    }else{
        axisY->setMin(0);
    }

    //增加新的点到曲线末端
//    if(device_connect){   //能接收到数据且关口未关闭
        series1->append(x, y1);
        series2->append(x, y2);
//    }

}

/**
 * @brief 深色主题按钮
 */
void ChartFigure::btn_chart_dark_click()
{
    chart->setTheme(QChart::ChartThemeBlueCerulean);    //深色主题设置
    chart->setTitleBrush(QBrush(QColor(0,0,255)));      //设置标题Brush
    chart->setTitleFont(QFont("微软雅黑"));              //设置标题字体
    series1->setPointLabelsColor(QColor(255,255,255));  //设置显示数值的颜色
    series1->setPen(QPen(QColor(60, 187, 251), 2));     //设置曲线的颜色和粗细
    Serial::serialClass->show_tips("图表已使用深色主题");
    Serial::serialClass->print_log("图表已使用深色主题", 0);
}

/**
 * @brief 浅色主题按钮
 */
void ChartFigure::btn_chart_light_click()
{
    chart->setTheme(QChart::ChartThemeBrownSand);       //浅色主题设置
    chart->setTitleBrush(QBrush(QColor(0,0,255)));      //设置标题Brush
    chart->setTitleFont(QFont("微软雅黑"));              //设置标题字体
    series1->setPointLabelsColor(QColor(0, 0, 204));    //设置显示数值的颜色
    series1->setPen(QPen(QColor(60, 187, 251), 2));     //设置曲线的颜色和粗细
    series2->setPen(QPen(QColor(51, 102, 204), 3));
    Serial::serialClass->show_tips("图表已使用浅色主题");
    Serial::serialClass->print_log("图表已使用浅色主题", 0);
}

/**
 * @brief 检查图表曲线显示状态
 */
void ChartFigure::check_chart_state()
{
    if(ui->cb_temper->isChecked() && ui->cb_flow->isChecked() ){
        ui->cb_show_all->setCheckState(Qt::Checked);
    }else if(!ui->cb_temper->isChecked() && !ui->cb_flow->isChecked() ){
        ui->cb_hide_all->setCheckState(Qt::Checked);
    }
}

/**
 * @brief 显示温度曲线
 */
void ChartFigure::cb_temper_show()
{
    if(ui->cb_temper->isChecked()){ //温度显示复选框按下
        series1->setVisible(true);
        ui->cb_hide_all->setCheckState(Qt::Unchecked);
        Serial::serialClass->show_tips("温度曲线以显示");
        Serial::serialClass->print_log("温度曲线以显示", 0);
    }else{
        series1->setVisible(false);
        ui->cb_show_all->setCheckState(Qt::Unchecked);
        Serial::serialClass->show_tips("温度曲线以隐藏");
        Serial::serialClass->print_log("温度曲线以隐藏", 0);
    }

    check_chart_state();
}

/**
 * @brief 显示流量曲线
 */
void ChartFigure::cb_flow_show()
{
    if(ui->cb_flow->isChecked()){ //流量显示复选框按下
        series2->setVisible(true);
        ui->cb_hide_all->setCheckState(Qt::Unchecked);
        Serial::serialClass->show_tips("流量曲线以显示");
        Serial::serialClass->print_log("流量曲线以显示", 0);
    }else{
        series2->setVisible(false);
        ui->cb_show_all->setCheckState(Qt::Unchecked);
        Serial::serialClass->show_tips("流量曲线以隐藏");
        Serial::serialClass->print_log("流量曲线以隐藏", 0);
    }

    check_chart_state();
}

/**
 * @brief 显示全部曲线
 */
void ChartFigure::cb_show_all()
{
    if(ui->cb_show_all->isChecked()){ //全部显示复选框按下
        series1->setVisible(true);
        series2->setVisible(true);
        Serial::serialClass->show_tips("所有曲线均以显示");
        Serial::serialClass->print_log("所有曲线均以显示", 0);
        ui->cb_temper->setCheckState(Qt::Checked);
        ui->cb_flow->setCheckState(Qt::Checked);
        ui->cb_hide_all->setCheckState(Qt::Unchecked);
    }
}

/**
 * @brief 隐藏全部曲线
 */
void ChartFigure::cb_hide_all()
{
    if(ui->cb_hide_all->isChecked()){ //全部不显示复选框按下
        series1->setVisible(false);
        series2->setVisible(false);
        Serial::serialClass->show_tips("所有曲线均以隐藏");
        Serial::serialClass->print_log("所有曲线均以隐藏", 0);
        ui->cb_temper->setCheckState(Qt::Unchecked);
        ui->cb_flow->setCheckState(Qt::Unchecked);
        ui->cb_show_all->setCheckState(Qt::Unchecked);
    }
}


ChartFigure::~ChartFigure()
{
    delete ui;
}
